//
//  WQNewFuturaView.swift
//  WeiBo
//
//  Created by 张德华 on 7/24/16.
//  Copyright © 2016 ZhangDeHua. All rights reserved.
//

import UIKit

class WQNewFuturaView: UIView,UIScrollViewDelegate {
    
    let scrollView = UIScrollView()
    
    let imageCount: Int = 4
    
    
    override init(frame: CGRect) {
        super.init(frame: UIScreen.mainScreen().bounds)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit{
        print("\(classForCoder) \(#function)")
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        removeFromSuperview()
    }
    
    func setupUI() {
        
        scrollView.delegate = self
        
        addSubview(scrollView)
        
        scrollView.snp_makeConstraints { (make) in
            make.size.equalTo(self)
        }
        
        for i in 0..<imageCount {
            let imageV: UIImageView = UIImageView(image: UIImage(named: "new_feature_\(i+1)"))
            imageV.frame = CGRectOffset(bounds, CGFloat(i) * bounds.width, 0)
            scrollView.addSubview(imageV)
        }
        
        scrollView.contentSize = CGSizeMake(SCREENSIZE.width * CGFloat(imageCount + 1), SCREENSIZE.height)
        scrollView.pagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        
        
      
        
        addSubview(pageControl)
        
        pageControl.snp_makeConstraints { (make) in
            make.bottom.equalTo(self).offset(-100)
            make.size.equalTo(CGSizeMake(100, 20))
            make.centerX.equalTo(self)
        }
        
        pageControl.currentPage = 0
        pageControl.currentPageIndicatorTintColor = ThemeColor
        pageControl.pageIndicatorTintColor = UIColor.brownColor()
        pageControl.numberOfPages = imageCount
        
        
        
    }
    
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
     
        let page = scrollView.contentOffset.x / bounds.width
        
        if page == CGFloat(imageCount) {
            removeFromSuperview()
        }
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / bounds.width + 0.5)
        pageControl.currentPage = page
        
        pageControl.hidden = (page == imageCount)
        
    }
    
    lazy var pageControl = UIPageControl()
    
    
}

//CollectionView实现
//class WQNewFuturaView: UIView {
//
//    override init(frame: CGRect) {
//        super.init(frame: UIScreen.mainScreen().bounds)
//        self.backgroundColor = UIColor.greenColor()
//        setupUI()
//    }
//
//    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
//
//    deinit{
//        print("\(classForCoder) \(#function)")
//    }
//
//    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//        removeFromSuperview()
//    }
//
//    private lazy var collectionView: UICollectionView = {
//
//
//        let flowLayout = UICollectionViewFlowLayout()
//        flowLayout.itemSize = SCREENSIZE
//        flowLayout.minimumLineSpacing = 0
//        flowLayout.minimumInteritemSpacing = 0
//        flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
//
//
//        let collectionView = UICollectionView(frame: self.frame, collectionViewLayout: flowLayout)
//        collectionView.dataSource = self
//        collectionView.showsVerticalScrollIndicator = false
//        collectionView.showsHorizontalScrollIndicator = false
//        collectionView.pagingEnabled = true
//
//        collectionView.registerClass(WQCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
//
//        return collectionView
//    }()
//
//
//
//
//}
//
//extension WQNewFuturaView: UICollectionViewDataSource {
//
//    func setupUI() {
//
//        addSubview(collectionView)
//
//    }
//
//
//    // MARK: - ************代理方法
//    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//        return 4
//    }
//
//    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//
//        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as? WQCollectionViewCell
//
//        cell?.imageIndex = indexPath.item
//
//        return cell!
//
//    }
//}
//
//class WQCollectionViewCell: UICollectionViewCell {
//
//
//
//    var imageIndex: Int = 0 {
//        didSet{
//            backgroundImage.image = UIImage(named: "new_feature_\(imageIndex+1)")
//
//        }
//    }
//
//
//    override init(frame: CGRect) {
//        super.init(frame: frame)
//        setupUI()
//    }
//
//    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
//
//    func setupUI() {
//
//        ///------ 添加背景图片
//        contentView.addSubview(backgroundImage)
//        contentView.addSubview(startButton)
//
//
//        ///------ 设置约束
//        backgroundImage.snp_makeConstraints { (make) in
//            make.top.leading.bottom.trailing.equalTo(0)
//        }
//        startButton.snp_makeConstraints { (make) in
//            make.centerX.equalTo(contentView)
//            make.bottom.equalTo(contentView).offset(-100)
//        }
//
//
//    }
//
//
//    // MARK: - ************懒加载
//    private lazy var backgroundImage: UIImageView = UIImageView()
//
//    private lazy var startButton: UIButton = {
//
//        let button = UIButton()
//        button.setTitle("欢迎回来", forState: UIControlState.Normal)
//        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
//        button.titleLabel?.font = UIFont.systemFontOfSize(17)
//        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
//        button.sizeToFit()
//
//        button.addTarget(self, action: #selector(WQCollectionViewCell.backButtonClicked), forControlEvents: UIControlEvents.TouchUpInside)
//
//        return button
//    }()
//
//    ///------ 按钮监听事件
//    func backButtonClicked() {
//        print("新特性->欢迎回来按钮")
//        let newFuturaView = WQNewFuturaView()
//        newFuturaView.removeFromSuperview()
//
//    }
//
//}
//

